package com.wanhe.base.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class NetWorkType {
	
	public static String TYPE_ERROR="ERROR";
	public static String TYPE_WIFI="WIFI";
	public static String TYPE_2G="2G";
	public static String TYPE_3G="3G";
	/**
	 * info.getSubtype()取值列表如下：
    * NETWORK_TYPE_CDMA 网络类型为CDMA
    * NETWORK_TYPE_EDGE 网络类型为EDGE
    * NETWORK_TYPE_EVDO_0 网络类型为EVDO0
    * NETWORK_TYPE_EVDO_A 网络类型为EVDOA
    * NETWORK_TYPE_GPRS 网络类型为GPRS
    * NETWORK_TYPE_HSDPA 网络类型为HSDPA
    * NETWORK_TYPE_HSPA 网络类型为HSPA
    * NETWORK_TYPE_HSUPA 网络类型为HSUPA
    * NETWORK_TYPE_UMTS 网络类型为UMTS
    *联通的3G为UMTS或HSDPA，移动和联通的2G为GPRS或EGDE，电信的2G为CDMA，电信的3G为EVDO
    *  */
	public static String getNetWorkType(Context context){
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if(cm == null) return TYPE_ERROR;
		NetworkInfo info = cm.getActiveNetworkInfo();
		if(info == null) return TYPE_ERROR;
		switch (info.getType()) {
		case ConnectivityManager.TYPE_WIFI:
			return TYPE_WIFI;
		case ConnectivityManager.TYPE_MOBILE:
			if(info.getSubtypeName().equals("GPRS")||info.getSubtypeName().equals("EDGE")||info.getSubtypeName().equals("CDMA")){
				Log.i("NetWorkState", "2G="+info.getSubtypeName());
				return  TYPE_2G;
			}
			if(info.getSubtypeName().equals("UMTS")||info.getSubtypeName().equals("HSDPA")||info.getSubtypeName().equals("EVDO")){
				Log.i("NetWorkState", "3G="+info.getSubtypeName());
				return  TYPE_3G;
			}
			return  info.getSubtypeName();
		default:
			return TYPE_ERROR;
		}
	}

	/**
	 * 网络类型  1.wifi 0 Mobile ,-1 网络连接不正常
	 * @param context
	 */
	public static int getNetworkTypeHint(Context context) {
		if (NetWorkType.TYPE_WIFI.equals(NetWorkType.getNetWorkType(context))) {
			return 1;
		}  else {
			if(!getNetWorkState(context,false)){
				return -1;
			}
			return 0;
		}
	}

	/**
	 * 网络状态 true connect,false disconnect
	 * @param context
	 * @param isShowToast
	 * @return
	 */
	public static boolean getNetWorkState(Context context, boolean isShowToast){
		ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		 NetworkInfo gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
	     NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
	     if(!gprs.isConnected() && !wifi.isConnected()) {
			// 断开连接
	    	 if(isShowToast){
//	    			ViewUtil.ShortToast(R.string.home_neterror_tips_netclose);
	    	 }
			return false;
		}
	     return true;
	}

    public static boolean pingNetworkConnect() {
        Process p = null;
        boolean result = false;
//        try {
//            p = Runtime.getRuntime().exec("ping -c 1 www.baidu.com ");
//            try {
//                result = (p.waitFor()==0);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        return true;
    }

}
